package com.vecharm.app.ui

import android.app.AlarmManager
import android.app.PendingIntent
import android.content.Context
import android.content.Intent
import android.os.Bundle
import android.support.annotation.CallSuper
import android.transition.Fade
import android.transition.Slide
import android.view.View
import com.ling.sdk.ui.BaseActivity
import com.ling.sdk.utils.Toasty
import com.vecharm.app.utils.TOKEN

abstract class BaseShowActivity : BaseActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        window.enterTransition = Slide()
        window.exitTransition = Slide()
    }

    companion object {
        var refCount = 0
    }

    var dataToken = ""

    @CallSuper
    override fun onInitView() {
        supportActionBar?.hide()
        getRootView().systemUiVisibility =
                View.SYSTEM_UI_FLAG_LOW_PROFILE or
                View.SYSTEM_UI_FLAG_FULLSCREEN or
                View.SYSTEM_UI_FLAG_LAYOUT_STABLE or
                View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION or
                View.SYSTEM_UI_FLAG_HIDE_NAVIGATION

        getRootView().keepScreenOn = true
    }

    @CallSuper
    override fun onStart() {
        super.onStart()
        refCount++
    }


    @CallSuper
    override fun onStop() {
        super.onStop()
        refCount--
//        checkBackHome()
    }

    private fun checkBackHome() {
        if (refCount == 0)
            restartApp()
    }

    private fun restartApp() {
        val restartIntent = PendingIntent.getActivity(this, 0, Intent(this, this::class.java).setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP).putExtra(TOKEN, dataToken), PendingIntent.FLAG_ONE_SHOT)
        //退出程序
        val mgr = getSystemService(Context.ALARM_SERVICE) as AlarmManager
        //app 退出后 10s 回来
        mgr.set(AlarmManager.RTC, System.currentTimeMillis() + 10 * 1000, restartIntent)
    }
}